/*
 * SalesMgrCB.java
 *
 * Created on April 20, 2007, 1:44 PM
 *
 * SalesMgrCB is responsible for recieving SalesRecords
 * sent from the Vending Machine(s) via the secure comm
 * link.
 */

package localSite;

import salesDB.*;
import java.io.Serializable;
import commpackage.SessionCallback;

/**
 *
 * @author Anne Harmeson
 */
public class SalesMgrCB extends SessionCallback
{
    private boolean stop;
    private SalesMgr mgr;
    
    /** Creates a new instance of TestCallback */
    public SalesMgrCB ( SalesMgr mgr )
    {
        this.mgr = mgr;
        stop = false;
    }
    
    public boolean NeedStop ()
    {
        return stop;
    }
    
    public void onSendError (int message_id, String message, boolean dropped_connection)
    {
        synchronized (this)
        {
            System.out.println ("Send Error " + message);
        }
    }
    
    public void onReceiveError (int message_id, String message, boolean dropped_connection )
    {
        synchronized (this)
        {
            System.out.println ("Receive Error " + message);
        }
    }
    
    public void onReceive ( int message_id, Serializable obj )
    {
        synchronized (this)
        {
            Class c = obj.getClass ();
            if( c.getName ().compareTo ( String.class.getName () ) == 0 )
            {
                String s = (String)obj;
                System.out.println ("Local Office Recieved: " + s.toString ());
                stop = true;
                
            }
            else if ( c.getName ().compareTo ( SalesRecord.class.getName () ) == 0 )
            {
                SalesRecord rec = (SalesRecord)obj;
                System.out.println ("Local Office recieved: " + rec.toString ());
                mgr.recordSale (rec);
                stop = true;
            }
            else
            {
                System.out.println ("Local Office received unknown obj: " + obj.toString ());
                stop= true;
            }
        }
    }
}
